/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mzm.ui.admin;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import org.jdesktop.swingx.decorator.HighlighterFactory;

import com.mzm.api.ProductInfoDto;
import com.mzm.api.RecommendationDto;

import dnl.util.ui.PopupUtils;
import dnl.util.ui.WindowUtils;

/**
 * 
 * @author daniel
 */
public class ProductListPanel extends javax.swing.JPanel {

	private AdminContext adminContext;

	private RecommendationEditingDialog recommendationEditingDialog = new RecommendationEditingDialog(
			(Frame) SwingUtilities.getWindowAncestor(this), true);

	/**
	 * Creates new form ProductListPanel
	 */
	public ProductListPanel() {
		initComponents();
		PopupUtils.addPopup(oscarTable, getPopup());
		oscarTable.setShowGrid(false, false);
		oscarTable.addHighlighter(HighlighterFactory.createSimpleStriping());
	}

	public void setAdminContext(AdminContext adminContext) {
		this.adminContext = adminContext;
		oscarTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				Integer productInfoId = getSelectedProductInfoId();
				ProductListPanel.this.adminContext.productSelected(productInfoId);
			}
		});
	}

	private Integer getSelectedProductInfoId() {
		int selectedRow = oscarTable.getSelectedRow();
		if (selectedRow < 0) {
			System.out.println("selectedRow=" + selectedRow + " !!!");
			return null;
		}
		Integer productInfoId = (Integer) oscarTable.getValueAt(selectedRow, 0);
		return productInfoId;
	}

	private JPopupMenu getPopup() {
		JPopupMenu pop = new JPopupMenu();
		JMenuItem addRecommendation = new JMenuItem(new AbstractAction("הוסף המלצה") {
			@Override
			public void actionPerformed(ActionEvent e) {
				openNewRecommendationDialog();
			}
		});
		JMenuItem deleteProduct = new JMenuItem(new AbstractAction("מחק מוצר") {
			@Override
			public void actionPerformed(ActionEvent e) {
				adminContext.getMzmAdminFacade().deleteProductInfo(
						adminContext.getSeletedProductInfoId());
				adminContext.refreshProductList();
			}
		});
		pop.add(addRecommendation);
		pop.add(deleteProduct);
		return pop;
	}

	public void setProductInfos(List<ProductInfoDto> infos) {
		oscarTable.setModel(new ProductsTableModel(infos));
	}

	private void openNewRecommendationDialog() {
		WindowUtils.setWindowSize(recommendationEditingDialog, .7, .9);
		WindowUtils.centerWindow(recommendationEditingDialog);
		recommendationEditingDialog.setVisible(true);
		if (!recommendationEditingDialog.wasCancelled()) {
			RecommendationDto recommendation = recommendationEditingDialog.getRecommendationDto();
			adminContext.getMzmAdminFacade().addRecommendation(getSelectedProductInfoId(),
					recommendation);
			adminContext.refreshRecommendations();
		}
	}

	private static class ProductsTableModel extends AbstractTableModel {

		private List<ProductInfoDto> infos;

		public ProductsTableModel(List<ProductInfoDto> infos) {
			this.infos = infos;
		}

		@Override
		public int getRowCount() {
			return infos.size();
		}

		@Override
		public int getColumnCount() {
			return 3;
		}

		@Override
		public String getColumnName(int columnIndex) {
			switch (columnIndex) {
			case 0:
				return "ID";
			case 1:
				return "Name";
			case 2:
				return "Type";
			}
			return null;
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			switch (columnIndex) {
			case 0:
				return Integer.class;
			case 1:
				return String.class;
			case 2:
				return String.class;
			}
			return null;
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			ProductInfoDto productInfo = infos.get(rowIndex);
			switch (columnIndex) {
			case 0:
				return productInfo.getId();
			case 1:
				return productInfo.getProductName();
			case 2:
				return productInfo.getProductType();
			}
			return null;
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane2 = new javax.swing.JScrollPane();
		oscarTable = new org.jdesktop.swingx.JXTable();

		setLayout(new java.awt.BorderLayout());

		oscarTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { {}, {}, {}, {} }, new String[] {

				}));
		jScrollPane2.setViewportView(oscarTable);

		add(jScrollPane2, java.awt.BorderLayout.CENTER);
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private javax.swing.JScrollPane jScrollPane2;
	private org.jdesktop.swingx.JXTable oscarTable;
	// End of variables declaration//GEN-END:variables
}
